<template>
    <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="downloadLoading" @click="handleDownload">{{this.buttonName}}导出 Excel</el-button>
</template>
 
<script>
import { login } from '@/api/login'

export default {
  name: 'ExportExcel',
  props: {
    list: {
      required: true,
      type: Array
    },
    buttonName:{
      required: false,
      type: String
    },
    tHeader: {
      required: true,
      type: Array
    },
    tValue: {
      required: true,
      type: Array
    },
    filename: {
      type: String,
      default: '导出数据'
    }
  },
  data() {
    return {
      downloadLoading: false,
      autoWidth: true,
      bookType: 'xlsx'
    }
  },
  methods: {
    handleDownload() {
      this.downloadLoading = true
      import('./ExportExcel.js').then(excel => {
        // if(this.tValue[location]){
        //     this.tValue.add(location.location)
        //     this.tValue[location]=this.tValue[location].desc
        // }
        // console.log(this.tValue)
        const data = this.formatJson(this.tValue, this.list)
        excel.export_json_to_excel({
          header: this.tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
            if(j == 'isUsed'){
                return v.isUsed === 0 ? '未使用' : '已使用'
            }else{
                return v[j]
            }
            // return v[j]
      }))
    }
  }
}
</script>